﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using log4net;
using Microsoft.ApplicationBlocks.Data;
using FunkeLab;
using System.Reflection;
using AnyOffice.general.ipanel;

namespace AnyOffice.general.ipanel
{
    public partial class menu : BasePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                try
                {
                    UserCache cache = new UserCache();
                    string str = "," + cache.FuncIdStr + ",";
                    string commandText = "select * from [dbo].[sys_menu] where MenuId in(";
                    commandText = commandText + "select distinct left(MenuID,2) from [dbo].[sys_function] where charindex(','+ CAST(FuncID as nvarchar(20)) + ',','" + str + "')>0)";
                    DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText).Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        for (int i = 0; i < table.Rows.Count; i++)
                        {
                            MyTreeViewItem item = new MyTreeViewItem();
                            item.Text = table.Rows[i]["MenuName"].ToString();
                            item.Image = "/images/menu/" + table.Rows[i]["Image"].ToString() + ".gif";
                            string str3 = "select * from [dbo].[sys_function] ";
                            str3 = (str3 + "where (charindex(','+ CAST(FuncId as nvarchar(20)) + ',','" + str + "')>0) ") + "and (len(MenuId)=4) and (left(MenuID,2) ='" + table.Rows[i]["MenuId"].ToString() + "') order by MenuID asc";
                            DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str3).Tables[0];
                            if (table2.Rows.Count > 0)
                            {
                                for (int j = 0; j < table2.Rows.Count; j++)
                                {
                                    MyTreeViewItem item2 = new MyTreeViewItem();
                                    item2.Text = table2.Rows[j]["FuncName"].ToString();
                                    item2.Image = "/images/menu/" + table2.Rows[j]["FuncImg"].ToString() + ".gif";
                                    if (table2.Rows[j]["FuncCode"].ToString().IndexOf("@") < 0)
                                    {
                                        if (table2.Rows[j]["FuncCode"].ToString().StartsWith("http://") || table2.Rows[j]["FuncCode"].ToString().StartsWith("file://"))
                                        {
                                            item2.Link = table2.Rows[j]["FuncCode"].ToString();
                                        }
                                        else
                                        {
                                            item2.Link = "../" + table2.Rows[j]["FuncCode"].ToString() + "/index.aspx";
                                        }
                                        item2.TargetFrame = "main";
                                    }
                                    string str4 = "select * from [dbo].[sys_function] ";
                                    str4 = (str4 + "where (charindex(','+ CAST(FuncId as nvarchar(20)) + ',','" + str + "')>0) ") + "and (len(MenuID)=6) and (left(MenuID,4) ='" + table2.Rows[j]["MenuID"].ToString() + "') order by MenuID asc";
                                    DataTable table3 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str4).Tables[0];
                                    if (table3.Rows.Count > 0)
                                    {
                                        for (int k = 0; k < table3.Rows.Count; k++)
                                        {
                                            MyTreeViewItem item3 = new MyTreeViewItem();
                                            item3.Text = table3.Rows[k]["FuncName"].ToString();
                                            item3.Image = "/images/menu/" + table3.Rows[k]["FuncImg"] + ".gif";
                                            if (table3.Rows[k]["FuncCode"].ToString().StartsWith("http://") || table3.Rows[k]["FuncCode"].ToString().StartsWith("file://"))
                                            {
                                                item3.Link = table3.Rows[k]["FuncCode"].ToString();
                                            }
                                            else
                                            {
                                                item3.Link = "../" + table3.Rows[k]["FuncCode"].ToString() + "/index.aspx";
                                            }
                                            item3.TargetFrame = "main";
                                            item2.Items.Add(item3);
                                        }
                                    }
                                    item.Items.Add(item2);
                                }
                            }
                            this.TreeView1.Items.Add(item);
                            this.TreeView1.EnableViewState = true;
                        }
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }

    }
}
